package com.caishichang.infosysdesign.service.impl;

import com.caishichang.infosysdesign.controller.vo.StrategyDetailVO;
import com.caishichang.infosysdesign.controller.vo.StrategyListVO;
import com.caishichang.infosysdesign.dao.StrategyMapper;
import com.caishichang.infosysdesign.entity.Strategy;
import com.caishichang.infosysdesign.enums.CommentStatus;
import com.caishichang.infosysdesign.service.StrategyService;
import com.caishichang.infosysdesign.util.MarkDownUtil;
import com.caishichang.infosysdesign.util.PageQueryUtil;
import com.caishichang.infosysdesign.util.PageResult;
import com.caishichang.infosysdesign.util.PatternUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
public class StrategyServiceImpl implements StrategyService {

    @Resource
    StrategyMapper strategyMapper;

    @Override
    public StrategyDetailVO getStrategyDetail(Long strategyId) {
        Strategy strategy = strategyMapper.selectStrategyById(strategyId);
        //不为空且状态为已发布
        StrategyDetailVO strategyDetailVO = getStrategyDetailVO(strategy);
        if (strategyDetailVO != null) {
            return strategyDetailVO;
        }
        return null;
    }

    @Override
    public Strategy getStrategy(Long strategyId) {
        return strategyMapper.selectStrategyById(strategyId);
    }

    @Override
    public StrategyDetailVO getStrategyDetailVO(Strategy strategy) {
        if (strategy != null && strategy.getStrategyStatus() == 1) {
            //增加浏览量
            strategy.setStrategyViewNumber(strategy.getStrategyViewNumber() + 1);
            strategyMapper.updateStrategy(strategy); // 写入数据库
            StrategyDetailVO strategyDetailVO = new StrategyDetailVO();
            BeanUtils.copyProperties(strategy, strategyDetailVO); // 属性拷贝
            strategyDetailVO.setStrategyContent(MarkDownUtil.mdToHtml(strategyDetailVO.getStrategyContent()));
            return strategyDetailVO;
        }
        return null;
    }

    @Override
    public PageResult getStrategiesPage(PageQueryUtil pageUtil) {
        List<Strategy> strategyList = strategyMapper.findStrategyList(pageUtil);
        int total = strategyMapper.getStrategyNumber(pageUtil);
        PageResult pageResult = new PageResult(strategyList, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public PageResult getStrategiesPageBySearch(String keyword, int page) {
        if (page > 0 && PatternUtil.validKeyword(keyword)) {
            Map param = new HashMap();
            param.put("page", page);
            param.put("limit", 9);
            param.put("keyword", keyword);
            param.put("blogStatus", 1);//过滤发布状态下的数据
            PageQueryUtil pageUtil = new PageQueryUtil(param);
            List<Strategy> StrategyList = strategyMapper.findStrategyList(pageUtil);
            List<StrategyListVO> strategyListVOS = getStrategyListVOsByStrategies(StrategyList);
            int total = strategyMapper.getStrategyNumber(pageUtil);
            PageResult pageResult = new PageResult(strategyListVOS, total, pageUtil.getLimit(), pageUtil.getPage());
            return pageResult;
        }
        return null;
    }

    @Override
    public PageResult getStrategiesForIndexPage(int page) {
        Map params = new HashMap();
        params.put("page", page);
        //每页8条
        params.put("limit", 8);
        params.put("strategyStatus", 1);//过滤发布状态下的数据
        PageQueryUtil pageUtil = new PageQueryUtil(params);
        List<Strategy> strategyList = strategyMapper.findStrategyList(pageUtil);
        List<StrategyListVO> strategyListVOS = getStrategyListVOsByStrategies(strategyList);
        int total = strategyMapper.getStrategyNumber(pageUtil);
        PageResult pageResult = new PageResult(strategyListVOS, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public List<StrategyListVO> getStrategyListVOsByStrategies(List<Strategy> strategyList) {
        List<StrategyListVO> strategyListVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(strategyList)) {
            for (Strategy strategy : strategyList) {
                StrategyListVO strategyListVO = new StrategyListVO();
                BeanUtils.copyProperties(strategy, strategyListVO);
                strategyListVOS.add(strategyListVO);
            }
        }

        return strategyListVOS;
    }

    @Override
    public boolean insertStrategy(Strategy strategy) {
        // 生成id
        strategy.setStrategyId(System.currentTimeMillis() * 1000 + (long) (Math.random() * 1000));
        // author id、title、content 已生成

        strategy.setStrategyCreateTime(new Date());
        strategy.setStrategyCommentStatus(CommentStatus.UNCOMMENTED.getValue());
        strategy.setStrategyViewNumber(0);
        strategy.setStrategyGoodNumber(0);
        strategy.setStrategyBadNumber(0);

        if (strategyMapper.insertStrategy(strategy) > 0) {
            return true;
        }

        return false;
    }

    @Override
    public boolean updateStrategy(Strategy strategy) {
        if (strategyMapper.updateStrategy(strategy) > 0) {
            return true;
        }

        return false;
    }

    @Override
    public boolean deleteStrategy(Long[] ids) {
        if (strategyMapper.deleteStrategy(ids) > 0) {
            return true;
        }
        return false;
    }
}
